import app
from google.appengine.ext import webapp
from google.appengine.api import users
from google.appengine.api import mail
from google.appengine.ext.webapp import template
from models import Feedback

class MainPage(webapp.RequestHandler):
    def get(self):
        self.response.out.write(template.render('templates/main.html', {}))
        
class FeedbackHandler(webapp.RequestHandler):
    def post(self):
        if not self.request.get("author") or not self.request.get("content") :
                self.redirect('/')
                return
        
        feedback = Feedback()
        if users.get_current_user():
            feedback.user = users.get_current_user()
        feedback.fill(self.request)
        feedback.put()
        
        body = """
        New message from site!
        Author: %s
        He wrote:
        %s
        """ % (feedback.author, feedback.content)
        mail.send_mail_to_admins('moorcock84@gmail.com',
                   'New message from site for you!',
                   body)
        self.redirect('/')
        
class LogoutHandler(webapp.RequestHandler):
    def get(self):
        self.redirect(users.create_logout_url('/'))

def main():
    app.start(
        ('/', MainPage),
        ('/feedback', FeedbackHandler),
        ('/logout', LogoutHandler)
    )

if __name__ == "__main__":
    main()